var  timer;
var socket = new WebSocket('ws://js.s.tansuyun.cn:8252')
// 打开Socket
socket.onopen = function (event) {

    if (window.socket) {

    }
};
// 监听消息
socket.onmessage = function (event) {
    var data = JSON.parse(event.data);
    console.log('Client received a message', data);
    /*switch (data.events) {
     case  'OPEN_LIGHT':
     var openID = document.getElementById('green');
     openID.style.display = 'none';//隐藏
     var openID1 = document.getElementById('red');
     openID1.style.display = 'block';//显示
     break;
     case  'CLOSE_LIGHT':
     var closeID = document.getElementById('red');
     closeID.style.display = 'none';
     var closeID1 = document.getElementById('green');
     closeID1.style.display = 'block';
     clearInterval(timer);    //清除定时器
     break;
     case 'FLICKER_LIGHT':
     var timer = setInterval(function () {
     var openID = document.getElementById('green');
     openID.style.display = 'none';//隐藏
     var openID1 = document.getElementById('red');
     openID1.style.display = 'block';//显示  灯亮
     setTimeout(function () {
     var closeID = document.getElementById('red');
     closeID.style.display = 'none';
     var closeID1 = document.getElementById('green');
     closeID1.style.display = 'block';//灯灭
     }, 500);
     if (data.status === 'CLOSE') {
     clearInterval(timer);    //清除定时器
     }
     }, 1000 * data.lateTime);
     break;
     }*/

    if (data.events === 'OPEN_LIGHT') {//打开
        window.clearInterval(timer);
        var openID = document.getElementById('green');
        openID.style.display = 'none';//隐藏
        var openID1 = document.getElementById('red');
        openID1.style.display = 'block';//显示
    }  else if (data.events === 'FLICKER_LIGHT') {//闪烁
        setTime();

    }else if (data.events === 'CLOSE_LIGHT') {//关闭
        window.clearInterval(timer);
        var closeID = document.getElementById('red');
        closeID.style.display = 'none';
        var closeID1 = document.getElementById('green');
        closeID1.style.display = 'block';
    }

    function setTime() {
         timer = setInterval(function () {
            var openID = document.getElementById('green');
            openID.style.display = 'none';//隐藏
            var openID1 = document.getElementById('red');
            openID1.style.display = 'block';//显示  灯亮
            setTimeout(function () {
                var closeID = document.getElementById('red');
                closeID.style.display = 'none';
                var closeID1 = document.getElementById('green');
                closeID1.style.display = 'block';//灯灭
            }, 500);
            if (data.events === 'CLOSE_LIGHT' ) {
                clearInterval(timer);    //清除定时器
            }
        }, 1000 * data.lateTime);
    }

};






